{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /home/xieyipeng/anaconda3/lib/python3.7/site-packages/tensorflow_core/python/compat/v2_compat.py:65: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "non-resource variables are not supported in the long term\n"
     ]
    }
   ],
   "source": [
    "import tensorflow.compat.v1 as tf\n",
    "import numpy as np\n",
    "tf.disable_v2_behavior()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.19999999\n",
      "9.999977\n"
     ]
    }
   ],
   "source": [
    "coefficients = np.array([[1.], [-20.], [100.]])\n",
    "\n",
    "\n",
    "w = tf.Variable(0, dtype=tf.float32)\n",
    "x = tf.placeholder(tf.float32, [3, 1])\n",
    "\n",
    "# TODO: cost function\n",
    "# cost = tf.add(tf.add(w ** 2, tf.multiply(-10., w)), 25)\n",
    "# cost = w ** 2 - 10 * w + 25\n",
    "cost = x[0][0] * w ** 2 + x[1][0] * w + x[2][0]\n",
    "train = tf.train.GradientDescentOptimizer(0.01).minimize(cost)\n",
    "\n",
    "init = tf.global_variables_initializer()\n",
    "session = tf.Session()\n",
    "session.run(init)\n",
    "\n",
    "session.run(train, feed_dict={x: coefficients})\n",
    "print(session.run(w))\n",
    "\n",
    "# session.run(train)\n",
    "# print(session.run(w))\n",
    "\n",
    "for i in range(1000):\n",
    "    session.run(train, feed_dict={x: coefficients})\n",
    "\n",
    "print(session.run(w))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
